#delimit;
clear all;
set more off;
set logtype text;
capture log close regressions_collapsed_IV_order;
** REPLACE FILE PATH WITH PATH TO RELEVANT REPLICATION FILES;
local fileloc = "~/KMS_REPLICATION";

log using `fileloc'/log_files/regressions_collapsed_IV_order.txt, replace name(regressions_collapsed_IV_order);

** Run regression prep;
do `fileloc'/all_regression_prep_hazard_collapsed.do `fileloc' _all;

eststo clear;

**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
** TABLE 9; 
**XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;

** Note dividing to make coefficients and standard errors easier for Stata to calculate;
foreach weather in max_temp windspeed humidS {;
	gen double `weather'sq = `weather'^2;
	gen double `weather'cu = `weather'^3/1000;
};

foreach weather in rain days_with_rain days_with_fog {;
	gen double `weather'sq = `weather'^2;
	gen double `weather'cu = `weather'^3;
};

** Instrumentation involves both total flow AND weather interactions – this code generates said interactions;
** Global "weather_linear" defined by the use of "run regression prep" above;
foreach weathervar in $weather_linear $weather_quadratic $weather_cubic {;
	gen double tot_flowIV_`weathervar' = tot_flow_base*`weathervar';
};

** Make coefficients easier to read;
replace died = died * 1000;

egen maxtraffic = max(tot_flow_base), by(mother_zip);
drop if maxtraffic == 0;

egen zip_rmonth = group(mother_zip event_month);

egen month_by_year = group(event_month event_year);

xi i.birth_quarter i.month_by_year;

compress;

**XXXXXXXXXXXXXX        PM10        XXXXXXXXXXXXXXXXXXX;

** No weather;
xtset zip_rmonth;
qui xtivreg2 died (weekly_pm10 = tot_flow_base) $triweekly_pm10 $weather1 $weather2 $controls $agespline _I* [fw = weight], ffirst  nocollin fe cluster(mother_zip);
_eststo;

capture matrix drop F;
matrix F = e(first);
estadd scalar PM10_APF = F[7,1]; 

** Temp;
qui xtivreg2 died (weekly_pm10 = tot_flow_base tot_flowIV_max_temp*) $triweekly_pm10 $weather1 $weather2 $controls $agespline _I* [fw = weight], ffirst  nocollin fe cluster(mother_zip);
_eststo;

capture matrix drop F;
matrix F = e(first);
estadd scalar PM10_APF = F[7,1]; 

** Humidity;
qui xtivreg2 died (weekly_pm10 = tot_flow_base tot_flowIV_max_temp* tot_flowIV_humidS*) $triweekly_pm10 $weather1 $weather2 $controls $agespline _I* [fw = weight], ffirst  nocollin fe cluster(mother_zip);
_eststo;

capture matrix drop F;
matrix F = e(first);
estadd scalar PM10_APF = F[7,1]; 

** Windspeed;
qui xtivreg2 died (weekly_pm10 = tot_flow_base tot_flowIV_max_temp* tot_flowIV_humidS* tot_flowIV_windspeed*) $triweekly_pm10 $weather1 $weather2 $controls $agespline _I* [fw = weight], ffirst  nocollin fe cluster(mother_zip);
_eststo;

capture matrix drop F;
matrix F = e(first);
estadd scalar PM10_APF = F[7,1]; 

** Rainfall;
qui xtivreg2 died (weekly_pm10 = tot_flow_base tot_flowIV_max_temp* tot_flowIV_humidS* tot_flowIV_windspeed* tot_flowIV_rain*) $triweekly_pm10 $weather1 $weather2 $controls $agespline _I* [fw = weight], ffirst  nocollin fe cluster(mother_zip);
_eststo;

capture matrix drop F;
matrix F = e(first);
estadd scalar PM10_APF = F[7,1]; 

** Days with rain;
qui xtivreg2 died (weekly_pm10 = tot_flow_base tot_flowIV_max_temp* tot_flowIV_humidS* tot_flowIV_windspeed* tot_flowIV_rain* tot_flowIV_days_with_rain) $triweekly_pm10 $weather1 $weather2 $controls $agespline _I* [fw = weight], ffirst  nocollin fe cluster(mother_zip);
_eststo;

capture matrix drop F;
matrix F = e(first);
estadd scalar PM10_APF = F[7,1]; 

** Days with fog;
qui xtivreg2 died (weekly_pm10 = tot_flow_base tot_flowIV_max_temp* tot_flowIV_humidS* tot_flowIV_windspeed* tot_flowIV_rain* tot_flowIV_days_with_rain tot_flowIV_days_with_fog) $triweekly_pm10 $weather1 $weather2 $controls $agespline _I* [fw = weight], ffirst  nocollin fe cluster(mother_zip);
_eststo;

capture matrix drop F;
matrix F = e(first);
estadd scalar PM10_APF = F[7,1]; 

esttab using `fileloc'/regs/KMS_IV_order.xls, 
	replace
	tab
	mlabels(, title)
	keep(weekly_pm10)
	label
	cells(b(fmt(%9.4f) star) se(fmt(%9.4f))) 
	starlevels(\text{$^{\ast}$} 0.10 \text{$^{\ast\ast}$} 0.05 \text{$^{\ast\ast\ast}$} 0.01)
	stats(N PM10_APF, label(Obs. F-stat) fmt(%9.0fc %9.2fc));	

log close regressions_collapsed_IV_order;
	
